package com.enqualcomm.kids.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.InputFilter;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.enqualcomm.kids.R;
import com.enqualcomm.kids.view.MyInputFilter;

import java.io.File;
import java.io.IOException;

import common.utils.DensityUtil;
import common.utils.PromptUtil;

/**
 * 家庭成员页面，编辑关注人name的对话框
 * @author yzq
 *
 */
public class SetRelationDialog extends Dialog {
    private TextView raletion_tv;
    private String picPath;
    private ImageView header_iv;
    private EditText mEdit;
    private CallBack callBack;

    public SetRelationDialog(Context context, TextView raletion_tv,String picPath,CallBack callBack) {
        super(context);
        this.raletion_tv = raletion_tv;
        this.picPath = picPath;
        this.callBack = callBack;
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_set_relation);

        setDialogAttributes();
        initDialogView();
    }
    private void setDialogAttributes() {
        Window dialogWindow = getWindow();
        dialogWindow.setGravity(Gravity.CENTER);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
//
        DisplayMetrics dm = new DisplayMetrics();
        dialogWindow.getWindowManager().getDefaultDisplay().getMetrics(dm);

        lp.width = dm.widthPixels - DensityUtil.dip2px(getContext(), 30); // 宽度
//		// lp.alpha = 0.9f; // 透明度
//
        dialogWindow.setAttributes(lp);
        dialogWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    }

    public void display(String picPath){
        this.picPath = picPath;

        if (new File(picPath).exists()){
            //说明是修改之后的，在sd卡
            header_iv.setImageBitmap(BitmapFactory.decodeFile(picPath));
        }else{
            try {
                header_iv.setImageBitmap(BitmapFactory.decodeStream(getContext().getAssets().open(picPath)));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    private void initDialogView() {
        mEdit = (EditText) findViewById(R.id.mEdit);
        mEdit.setFilters(new InputFilter[]{new MyInputFilter()});
        String s = raletion_tv.getText().toString();
        if (s.length()!=0){
            mEdit.setText(s);
            mEdit.setSelection(s.length());
        }

        header_iv = (ImageView) findViewById(R.id.header_iv);
        header_iv.setOnClickListener(callBack);

        if (new File(picPath).exists()){
            //说明是修改之后的，在sd卡
            header_iv.setImageBitmap(BitmapFactory.decodeFile(picPath));
        }else{
            try {
                header_iv.setImageBitmap(BitmapFactory.decodeStream(getContext().getAssets().open(picPath)));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


        //3.初始化确定，取消按钮
        findViewById(R.id.cancelBtn).setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View arg0, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {

                    dismiss();
                }
                return false;
            }
        });

        findViewById(R.id.sureBtn).setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View view, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    String str = mEdit.getText().toString().trim();
                    if (str.length()==0){
                        PromptUtil.toast(getContext(),R.string.input_empty);
                    }else {
                        raletion_tv.setText(str);
                        callBack.inputOver(picPath);
                        dismiss();
                    }
                }
                return false;
            }
        });

    }

    public interface CallBack extends View.OnClickListener{
        void inputOver(String picPath);
    }

}
